import Vue from 'vue'
import Router from 'vue-router'
import Layout from '@/layout'
import EmptyLayout from '@/layout/EmptyLayout' // 导入 EmptyLayout 布局
import CaseDetail from '@/views/UserView/home/CaseDetail'

Vue.use(Router)

// 常量路由数组，表示网站的基本路由配置
export const constantRoutes = [
  {
    path: '/', // 独立的前端路由
    component: EmptyLayout, // 使用 EmptyLayout 布局
    name: 'home',
    hidden: true, // 隐藏该路由
    meta: {
      title: '首页'
    },

    children: [
      {
        path: '', // 使用空路径，表示此路由的根页面
        name: 'HomeAI',
        component: () => import('@/views/UserView/home/index'), // 导入首页组件
        meta: { title: '首页' } // 路由元信息
      },
      {
        path: 'case-detail/:id',
        name: 'CaseDetail',
        component: CaseDetail,
        meta: { title: '案例详情' }
      }
    ]
  },
  {
    path: '/login', // 登录页面路由
    component: () => import('@/views/SystemView/login/index'), // 登录页面组件
    hidden: true // 隐藏该路由
  },
  {
    path: '/callback', // 回调页面路由
    component: () => import('@/views/SystemView/callback/index'), // 回调页面组件
    hidden: true // 隐藏该路由
  },
  {
    path: '/404', // 404 页面路由
    component: () => import('@/views/404'), // 404 页面组件
    hidden: true // 隐藏该路由
  },
  {
    path: '/dashboard', // 根路由
    component: Layout, // 布局组件
    redirect: '/dashboard', // 重定向到 dashboard 路由
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/SystemView/dashboard/index'), // dashboard 组件
      meta: { title: '首页', icon: 'dashboard', hideMenu: true } // 路由元信息
    }]
  },
  // AI助手介绍页面路由 - 修改为支持动态参数（隐藏菜单）
  {
    path: '/agent-intro/:id?',
    component: EmptyLayout,
    name: 'AIAgentIntro',
    meta: { title: 'AI助手介绍', icon: 'el-icon-document', hideMenu: true },
    children: [{
      path: '',
      name: 'AIAgentIntroDetail',
      component: () => import('@/views/UserView/AIAgentIntro/index'),
      meta: { title: 'AI助手介绍' }
    }, ]
  },
  {
    path: '/guixku',
    component: EmptyLayout,
    name: 'GuixkuIntro',
    meta: { title: '桂小库智能助手介绍', hideMenu: true },
    children: [{
      path: '',
      name: 'GuixkuIntroDetail',
      component: () => import('@/views/UserView/AIAgentIntro/GuixkuIntro'),
      meta: { title: '桂小库智能助手介绍' }
    }]
  },
  // AI知识文档页面路由 - 使用EmptyLayout以集成首页导航栏
  {
    path: '/user/ai-documentation',
    component: EmptyLayout,
    name: 'AIDocumentation',
    meta: { title: 'AI知识文档', icon: 'el-icon-document' },
    children: [{
      path: '',
      name: 'AIDocumentationDetail',
      component: () => import('@/views/UserView/home/AIDocumentation.vue'),
      meta: { title: 'AI知识文档' }
    }]
  },
  {
    path: '/agent-function',
    component: EmptyLayout,
    name: 'AgentFunction',
    hidden: true,
    meta: { title: '智能体功能页' },
    children: [
      {
        path: 'LGB_Agent1',
        name: 'LGB_Agent1',
        component: () => import('@/views/UserView/AgentFunctionPage/MeetingMinutes.vue'),
        meta: { title: '会议纪要生成助手' }
      },
      {
        path: 'LGB_AgentCode',
        name: 'LGB_AgentCode',
        component: () => import('@/views/UserView/AgentFunctionPage/CreditReference.vue'),
        meta: { title: '征信知识查询助手' }
      },
      {
        path: 'CreditReference',
        name: 'CreditReference',
        component: () => import('@/views/UserView/AgentFunctionPage/CreditReference.vue'),
        meta: { title: '征信问答助手' }
      },
      {
        path: 'LGB_AgentLegal',
        name: 'LGB_AgentLegal',
        component: () => import('@/views/UserView/AgentFunctionPage/LGB_AgentLegal.vue'),
        meta: { title: 'Ai助手' }
      },
      {
        path: 'three_step_translator',
        name: 'ThreeStepTranslator',
        component: () => import('@/views/UserView/AgentFunctionPage/three_step_translator.vue'),
        meta: { title: '三步翻译', appName: 'three_step_translator' }
      },
      {
        path: 'AIchat',
        name: 'AIchat',
        component: () => import('@/views/UserView/AgentFunctionPage/AIchat.vue'),
        meta: { title: 'Ai助手' }
      },
      {
        path: 'AdminLawCheck',
        name: 'AdminLawCheck',
        component: () => import('@/views/UserView/AgentFunctionPage/AdminLawCheck.vue'),
        meta: { title: 'Ai助手' }
      },
      {
        path: 'ApiKeyManage',
        name: 'ApiKeyManage',
        component: () => import('@/views/UserView/ApiKeyManage/index'),
        meta: { title: 'App映射管理' }
      },
      {
        path: 'Hui2Chat',
        name: 'Hui2Chat',
        component: () => import('@/views/UserView/AgentFunctionPage/Hui2Chat.vue'),
        meta: { title: '桂小库智能助手' }
      },
      {
        path: 'Enf_Inspection',
        name: 'EnfInspection',
        component: () => import('@/views/UserView/AgentFunctionPage/Enf_Inspection.vue'),
        meta: { title: '行政执法检查助手' }
      },
      {
        path: 'TreasuryAIchat',
        name: 'TreasuryAIchat',
        component: () => import('@/views/UserView/AgentFunctionPage/TreasuryAIchat.vue'),
        meta: { title: '桂小库智能助手' }
      },
      {
        path: 'PDFRecognition',
        name: 'PDFRecognition',
        component: () => import('@/views/UserView/AgentFunctionPage/PDFRecognition.vue'),
        meta: { title: 'PDF文件识别与对比', appName: 'pdfocr' }
      }
    ]
  },
  {
    path: '/ai', // AI对话路由
    component: Layout,
    name: 'AI',
    meta: { title: 'AI助手', icon: 'el-icon-service' },
    children: [
      {
        path: 'chat',
        name: 'AiChat',
        component: () => import('@/views/ai/chat/index'),
        meta: { title: 'AI对话', icon: 'el-icon-chat-dot-round' }
      },
      {
        path: 'test',
        name: 'AiTest',
        component: () => import('@/views/ai/chat/index1'),
        meta: { title: '流式测试', icon: 'el-icon-test' }
      },
      {
        path: 'debug',
        name: 'AiDebug',
        component: () => import('@/views/ai/chat/debug-test'),
        meta: { title: '调试测试', icon: 'el-icon-debug' }
      }
    ]
  },// 聊天界面路由
  {
    path: '/chat',
    component: EmptyLayout,
    name: 'Chat',
    meta: { title: 'AI聊天助手' },
    children: [
      {
        path: '',
        name: 'ChatPage',
        component: () => import('@/views/UserView/chat/index'),
        meta: { title: 'AI聊天助手' }
      }
    ]
  },
  // 新增：模型科普页面路由
  {
    path: '/model-intro',
    component: EmptyLayout,
    name: 'ModelIntro',
    redirect: '/model-intro/language',
    meta: { title: '模型科普', icon: 'el-icon-notebook-2' },
    children: [
      {
        path: 'language',
        name: 'LanguageModels',
        component: () => import('@/views/UserView/ModelIntro/LanguageModels.vue'),
        meta: { title: '语言模型' },
        props: (route) => ({
          presetFilter: route.query.preset || null
        })
      },
      {
        path: 'language/detail/:id',
        name: 'ModelDetail',
        component: () => import('@/views/UserView/ModelIntro/ModelDetail.vue'),
        meta: { title: '模型详情' }
      }
    ]
  },
  // {
  //   path: '/salary-detail', // 独立的薪资详情路由
  //   component: EmptyLayout, // 使用 EmptyLayout 布局
  //   name: 'SalaryDetail',
  //   hidden: true, // 隐藏该路由
  //   meta: {
  //     title: '薪资详情',
  //     icon: 'el-icon-document' // 添加合适的图标
  //   },

  //   children: [
  //     {
  //       path: '', // 使用空路径，表示此路由的根页面
  //       name: 'SalaryDetailPage',
  //       component: () => import('@/views/salary/salaryDetail'), // 导入薪资详情页面组件
  //       meta: { title: '薪资详情' }, // 路由元信息

  //     }
  //   ]
  // },
  // {
  //   path: '/upload', // 文件上传路由
  //   component: Layout,
  //   name: 'Upload',
  //   meta: { title: '文件上传', icon: 'el-icon-upload' }, // 路由元信息
  //   children: [
  //     {
  //       path: 'uploadfile',
  //       name: 'Uploadfile',
  //       component: () => import('@/views/upload/uploadfile/uploadsalary'), // 导入文件上传组件
  //       meta: { title: '文件上传',
  //       icon: 'table',
  //      } // 路由元信息
  //     }
  //   ]
  // },

  {
    name: 'Acl',
    path: '/acl',
    component: Layout,
    redirect: '/acl/user/list',
    meta: {
      title: '权限管理',
      icon: 'el-icon-lock'
    },
    children: [
      {
        name: 'Org',
        path: 'org',
        component: () => import('@/views/SystemView/acl/org'),
        meta: {
          title: '组织管理',
        },
      },
      {        name: 'User',
        path: 'user/list',
        component: () => import('@/views/SystemView/acl/user/index'),
        meta: {
          title: '用户管理',
        },
      },
      {
        name: 'UserAuth',
        path: 'user/auth/:id',
        component: () => import('@/views/SystemView/acl/user/userAuth'),
        meta: {
          activeMenu: '/acl/user/list',
          title: '用户授权',
        },
        hidden: true,
      },
      {
        name: 'Role',
        path: 'role/list',
        component: () => import('@/views/SystemView/acl/role/list'),
        meta: {
          title: '角色管理',
        },
      },
      {
        name: 'RoleAuth',
        path: 'role/auth/:id',
        component: () => import('@/views/SystemView/acl/role/roleAuth'),
        meta: {
          activeMenu: '/acl/role/list',
          title: '角色授权',
        },
        hidden: true,
      },
      {
        name: 'Permission',
        path: 'permission/list',
        component: () => import('@/views/SystemView/acl/permission/list'),
        meta: {
          title: '菜单管理',
        },
      },
    ]
  },
  { path: '*', redirect: '/404', hidden: true }, // 未匹配到路由时重定向到 404 页面
  // Dify配置管理路由
  {
    path: '/system',
    component: Layout,
    name: 'System',
    meta: { title: '系统管理', icon: 'el-icon-setting' },
    children: [
      {
        path: 'dify-config',
        name: 'DifyConfig',
        component: () => import('@/views/SystemView/difymanage/index'),
        meta: { title: 'Dify配置管理', icon: 'el-icon-bot' }
      }
    ]
  },
  // Dify用户使用页面路由
  {
    path: '/dify-view',
    component: EmptyLayout,
    name: 'DifyView',
    meta: { title: 'AI智能体', icon: 'el-icon-bot' },
    children: [
      {
        path: '',
        name: 'DifyViewIndex',
        component: () => import('@/views/UserView/difyView/index'),
        meta: { title: 'AI智能体' }
      }
    ]
  }
]

const createRouter = () => new Router({
  mode: 'history', // 使用 HTML5 History 模式
  scrollBehavior: () => ({ y: 0 }), // 页面滚动行为
  routes: constantRoutes // 将常量路由数组传入路由实例
})

const router = createRouter()

// 重置路由方法，用于修改权限等情况
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher
}

export default router
